package rm.member.business.service.impl;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import rm.cq.common.core.consts.ResponseData;
import rm.cq.common.core.enums.ResponseCodeEnum;
import rm.member.business.domain.BO.ShoppingInfoBO;
import rm.member.business.domain.DO.*;
import rm.member.business.domain.DTO.*;
import rm.member.business.domain.convert.*;
import rm.member.business.repository.MZGiftRepository;
import rm.member.business.service.GiftGoodsForTerminalService;
import javax.json.JsonObject;
import java.util.ArrayList;
import java.util.List;

import rm.member.common.entity.mz.MZGiftConst;

public class GiftGoodsForTerminalServiceImpl implements GiftGoodsForTerminalService {

    /**
     * 接口一实现
     * @param orderInfoDTO
     * @return
     */
    @Override
    public ResponseData<GoodsPromotionPageDTO> getGoodsPromotionPage(OrderInfoDTO orderInfoDTO) {
        //调用方法-DTO转BO
        ShoppingInfoBO shoppingInfoBO = getShoppingInfoBO(orderInfoDTO);

        //查库
        MZGiftRepository mzGiftRepository = new MZGiftRepository();
        List<DynamicObject> dynamicObjects = mzGiftRepository.findAllBySelect(shoppingInfoBO);
        if(dynamicObjects.size()<1){
            return new ResponseData(ResponseCodeEnum.FAILED.getCode(), "没有数据",null);
        }
        //对活动方案进行筛选
        //会员等级
        /*long memberLevelId = mzGiftRepository.getMemberLevelId()*/






        //拼装原品列表
        //取活动方案一个的信息
        DynamicObject dynamicObject = dynamicObjects.get(0);
        //调用方法-获取单个活动的详细信息
        GoodsPromotionDetailDO goodsPromotionDetailDO = getGoodsPromotionDetailDO(dynamicObject);
        //DO-->DTO
        GoodsPromotionDetailDTO goodsPromotionDetailDTO = GoodsPromotionDetailDOToDTO.INSTANCE.domain2dto(goodsPromotionDetailDO);

        //所有活动名和活动id
        List<GoodsPromotionItemDTO> goodsPromotionItemDTOList = new ArrayList<>();
        GoodsPromotionItemDO goodsPromotionItemDO = null;
        for (DynamicObject dynamicObject1 : dynamicObjects) {
            goodsPromotionItemDO = new GoodsPromotionItemDO();
            goodsPromotionItemDO.setPromotionName(dynamicObject1.getString("name"));
            goodsPromotionItemDO.setPromotionUid(dynamicObject1.getLong("id") + "");
            GoodsPromotionItemDTO goodsPromotionItemDTO = GoodsPromotionItemDOToDTO.INSTANCE.domain2dto(goodsPromotionItemDO);
            goodsPromotionItemDTOList.add(goodsPromotionItemDTO);
        }


        GoodsPromotionPageDTO goodsPromotionPageDTO = new GoodsPromotionPageDTO();
        goodsPromotionPageDTO.setGoodsPromotionDetailDTO(goodsPromotionDetailDTO);
        goodsPromotionPageDTO.setGoodsPromotionItemDTOList(goodsPromotionItemDTOList);

        ResponseData responseData = new ResponseData(ResponseCodeEnum.OK.getCode(), "查询成功",goodsPromotionPageDTO);
        return responseData;
    }

    /**
     * 接口二实现
     * @param orderInfoDTO
     * @param activityUid
     * @return
     */
    @Override
    public ResponseData<GoodsPromotionDetailDTO> getGoodsPromotionDetail(OrderInfoDTO orderInfoDTO,String activityUid) {
        //调用方法-DTO转BO
        //ShoppingInfoBO shoppingInfoBO = getShoppingInfoBO(orderInfoDTO);

        //查库
        MZGiftRepository mzGiftRepository = new MZGiftRepository();
        DynamicObject dynamicObject = mzGiftRepository.findActivityByUid(activityUid);
        if(dynamicObject==null){
            return new ResponseData(ResponseCodeEnum.FAILED.getCode(), "没有数据",null);
        }
        GoodsPromotionDetailDO goodsPromotionDetailDO = getGoodsPromotionDetailDO(dynamicObject);
        //DO-->DTO
        GoodsPromotionDetailDTO goodsPromotionDetailDTO = GoodsPromotionDetailDOToDTO.INSTANCE.domain2dto(goodsPromotionDetailDO);

        ResponseData responseData = new ResponseData(ResponseCodeEnum.OK.getCode(), "查询成功",goodsPromotionDetailDTO);
        return responseData;
    }

    /**
     * 接口三实现
     * @param checkGiftGoodsDTO
     * @return
     */
    @Override
    public ResponseData<List<JsonObject>> checkGiftGoods(CheckGiftGoodsDTO checkGiftGoodsDTO) {
        return null;
    }

    /**
     * DTO-->BO
     * @param orderInfoDTO
     * @return
     */
    public ShoppingInfoBO getShoppingInfoBO(OrderInfoDTO orderInfoDTO){
        //DTO-->DO
        ChangeDepositLgDetailDO changeDepositLgDetailDO = OrderInfoToCD.INSTANCE.domain2dto(orderInfoDTO);
        MemberChangeInfoDetailDo memberChangeInfoDetailDo = OrderInfoToMC.INSTANCE.domain2dto(orderInfoDTO);
        MZSupplementOneDo mzSupplementOneDo = OrderInfoToMZS.INSTANCE.domain2dto(orderInfoDTO);
        OrderGoodsDetailListDo orderGoodsDetailListDo = OrderInfoToOGDL.INSTANCE.domain2dto(orderInfoDTO);
        //DO-->BO
        ShoppingInfoBO shoppingInfoBO = new ShoppingInfoBO();
        shoppingInfoBO.setChangeDepositLgDetailDO(changeDepositLgDetailDO);
        shoppingInfoBO.setMemberChangeInfoDetailDo(memberChangeInfoDetailDo);
        shoppingInfoBO.setMzSupplementOneDo(mzSupplementOneDo);
        shoppingInfoBO.setOrderGoodsDetailListDo(orderGoodsDetailListDo);
        return shoppingInfoBO;
    }

    /**
     * 获取一个方案的活动信息
     * @param dynamicObject
     * @return
     */
    public GoodsPromotionDetailDO getGoodsPromotionDetailDO(DynamicObject dynamicObject){
        GoodsPromotionDetailDO goodsPromotionDetailDO = new GoodsPromotionDetailDO();

        List<String> activeGoodsList = new ArrayList<String>();
        //活动商品是否全部，全部:1
        Object choose = dynamicObject.get(MZGiftConst.ENTITY_ALLACTIVATEGOODS_NAME);
        String chooseStr = (String) choose;
        if ("1".equals(chooseStr)){
            //获取全部原品id
            //查库获取所有活动原品ID

        }else if ("2".equals(chooseStr)){
            //参与活动部分商品
            DynamicObjectCollection acGoods = dynamicObject.getDynamicObjectCollection(MZGiftConst.ENTITY_ACTIVATEGOODS_NAME);
            Object[] arrays = acGoods.toArray();
            for (Object arr:arrays) {
                DynamicObject arrD = (DynamicObject) arr;
                long aLong = arrD.getDynamicObject(MZGiftConst.ACTIVATEGOODS_NO).getLong("id");
                activeGoodsList.add(""+aLong);
            }

        }

        List<String> activeGoodsNNOList = new ArrayList<String>();
        //不参与活动商品
        DynamicObjectCollection acFGoods = dynamicObject.getDynamicObjectCollection(MZGiftConst.ENTITY_FACTIVATEGOODS_NAME);
        Object[] acFGoodsArrays = acFGoods.toArray();
        if(acFGoodsArrays.length>0){
            for (Object arr:acFGoodsArrays) {
                DynamicObject arrD = (DynamicObject) arr;
                long aLong = arrD.getDynamicObject(MZGiftConst.FACTIVATEGOODS_NO).getLong("id");
                //String id = arrD.getString("id");
                activeGoodsNNOList.add(""+aLong);
            }
        }
        if(activeGoodsNNOList.size()>0){
            activeGoodsList.removeAll(activeGoodsNNOList);
        }
        //添加活动商品号到详情
        goodsPromotionDetailDO.setActiveGoodsNoList(activeGoodsList);

        //拼装商品促销活动
        GoodsPromotionDO goodsPromotionDO = new GoodsPromotionDO();
        goodsPromotionDetailDO.setGoodsPromotionDO(goodsPromotionDO);
        //赠品价格限制，写死，前端砍掉了
        goodsPromotionDO.setGiftPriceLimit(0);
        //享受次数
        String goodsenjoytimes = dynamicObject.getString(MZGiftConst.GOODSENJOYTIMES);
        if ("不限制".equals(goodsenjoytimes)) {
            //不限制享受次数
            goodsPromotionDO.setGivenCounts(-1);
        } else {
            goodsPromotionDO.setGivenCounts((Integer)dynamicObject.getInt(MZGiftConst.INPUTENJOYTIMES2));
        }
        //赠送模式,赠送数量
        String givenModel = dynamicObject.getString(MZGiftConst.GIVENGOODSCOUNT);
        if ("任选".equals(givenModel)) {
            goodsPromotionDO.setGivenModel(0);
            goodsPromotionDO.setGivenTypeCounts(dynamicObject.getInt(MZGiftConst.GOODSTYPECOUNT));
        } else if ("全部赠送".equals(givenModel)) {
            goodsPromotionDO.setGivenModel(1);
            goodsPromotionDO.setGivenTypeCounts(-1);
        }
        //活动名称
        goodsPromotionDO.setPromotionName(dynamicObject.getString(MZGiftConst.NAME));
        //活动uid
        goodsPromotionDO.setPromotionUid(dynamicObject.getLong("id") + "");
        //原价恢复零售价；1：是；0：否
        String isrecoverprice = dynamicObject.getString(MZGiftConst.ISRECOVERPRICE);
        if ("是".equals(isrecoverprice)) {
            goodsPromotionDO.setRecoverySalePriceFlag(1);
        } else if ("否".equals(isrecoverprice)) {
            goodsPromotionDO.setRecoverySalePriceFlag(0);
        }
        //赠品详情
        List<GiftGoodsDO> giftGoodsDOList = new ArrayList<>();
        goodsPromotionDetailDO.setGiftGoodsDOList(giftGoodsDOList);
        GiftGoodsDO giftGoodsDO = null;
        DynamicObjectCollection giftGoods = dynamicObject.getDynamicObjectCollection(MZGiftConst.ENTITY_FREEGOODS_NAME);
        Object[] giftGoodsArrays = giftGoods.toArray();
        if (giftGoodsArrays.length > 0) {
            for (Object arr : giftGoodsArrays) {
                DynamicObject arrD = (DynamicObject) arr;
                DynamicObject object = arrD.getDynamicObject(MZGiftConst.GOODSCODE);
                giftGoodsDO = new GiftGoodsDO();
                giftGoodsDOList.add(giftGoodsDO);
                //企业id
                /*long entId = arrD.getDynamicObject(MZGiftConst.MANUFACTURER).getLong("id");
                giftGoodsDO.setEntId(entId + "");*/
                giftGoodsDO.setEntId(object.getDynamicObject("factory").getString("id"));
                //生产厂家
                /*giftGoodsDO.setFactoryName(arrD.getString(MZGiftConst.MANUFACTURER));*/
                giftGoodsDO.setFactoryName(object.getDynamicObject("factory").getString("name"));
                //商品名称
                /*giftGoodsDO.setGoodsName(arrD.getString(MZGiftConst.GOODSNAME));*/
                giftGoodsDO.setGoodsName(object.getString("name"));
                //商品规格
                /*giftGoodsDO.setGoodsSpec(arrD.getString(MZGiftConst.SPECS));*/
                giftGoodsDO.setGoodsSpec(object.getString("spec"));
                //商品uid
                /*long goodsUid = arrD.getDynamicObject(MZGiftConst.GOODSNAME).getLong("id");
                giftGoodsDO.setGoodsUid(goodsUid + "");*/
                giftGoodsDO.setGoodsUid(object.getLong("id")+"");
                //单位编码
                /*giftGoodsDO.setGoodsUnitCode(arrD.getDynamicObject(MZGiftConst.UNITNAME).getString("number"));*/
                giftGoodsDO.setGoodsUnitCode(object.getDynamicObject("uom").getString("number"));
                //单位名称
                /*giftGoodsDO.setGoodsUnitName(arrD.getString(MZGiftConst.UNITNAME));*/
                giftGoodsDO.setGoodsUnitName(object.getDynamicObject("uom").getString("name"));
                //换购单价
                giftGoodsDO.setIncreasePrice(arrD.getBigDecimal(MZGiftConst.ADDPRICE));
                //货号
                giftGoodsDO.setItemNo(object.getString("number"));
                //阶梯号
                giftGoodsDO.setLadder(-1);
                //单次赠送数量
                giftGoodsDO.setPerGivenQuantity(arrD.getBigDecimal(MZGiftConst.GIVEQTY));
                //零售价
                giftGoodsDO.setSalePrice(object.getBigDecimal("price"));
                //优惠卷id  被砍了  返回-1
                giftGoodsDO.setUid("-1");

            }
        }
        return goodsPromotionDetailDO;
    }

}
